package linker.file;

import linker.account.ManageAccount;
import linker.plugin.PluginShare;

import java.io.File;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;

/**
 * 
 * @version linker 0.93
 * @author Jianfeng jianfeng.linker@gmail.com
 * 
 */
public class FileTableModel extends DefaultTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Vector<Object> fileVector = new Vector<Object>();

	public FileTableModel() {
		addColumn("File Name");
		addColumn("Sender");
		addColumn("Reciever");
		addColumn("Status");
		addColumn("%");
		addColumn("Saved Path");
	}

	public void addSendFile(OutgoingFileTransfer sendFileRequest) {
		Vector<String> temp = new Vector<String>();

		temp.add(sendFileRequest.getFileName());
		temp.add(new String("You"));
		temp.add(sendFileRequest.getPeer());
		temp.add(new String("Sending"));
		temp.add(new String("0.0"));
		temp.add(sendFileRequest.getFilePath());
		addRow(temp);

		fileVector.add(sendFileRequest);
	}

	public void addRecieveFile(FileTransferRequest request) {
		Vector<String> temp = new Vector<String>();
		temp.add(request.getFileName());
		temp.add(request.getRequestor());
		temp.add(new String("You"));
		temp.add(new String("Wait to Accept"));
		temp.add(new String("0.0"));
		temp.add("");
		addRow(temp);

		fileVector.add(request);
	}

	public void cancel(int selectedRow) {
		if (selectedRow < 0)
			return;

		if (fileVector.elementAt(selectedRow) instanceof OutgoingFileTransfer) {
			((OutgoingFileTransfer) fileVector.elementAt(selectedRow)).cancel();

			setValueAt("Canceled", selectedRow, 3);
		} else if (fileVector.elementAt(selectedRow) instanceof IncomingFileTransfer) {
			((IncomingFileTransfer) fileVector.elementAt(selectedRow)).cancel();

			setValueAt("Canceled", selectedRow, 3);
		} else if (fileVector.elementAt(selectedRow) instanceof FileTransferRequest) {
			((FileTransferRequest) fileVector.elementAt(selectedRow)).reject();

			setValueAt("Rejected", selectedRow, 3);
		}
	}

	public void refresh() {
		int i = 0;

		for (i = 0; i < fileVector.size(); i++) {
			if (fileVector.elementAt(i) instanceof OutgoingFileTransfer) {
				OutgoingFileTransfer transfer = (OutgoingFileTransfer) fileVector
						.elementAt(i);

				setValueAt(String.valueOf(transfer.getProgress() * 100), i, 4);
				if (transfer.isDone())
					setValueAt("Done", i, 3);
			} else if (fileVector.elementAt(i) instanceof IncomingFileTransfer) {
				IncomingFileTransfer transfer = (IncomingFileTransfer) fileVector
						.elementAt(i);

				setValueAt(String.valueOf(transfer.getProgress() * 100), i, 4);

				if (transfer.isDone()) {
					if (((String) getValueAt(i, 5)).indexOf("plugins") > -1)
						PluginShare.recivePlugin((String) getValueAt(i, 5));
					setValueAt("Done", i, 3);
				}
			}
		}
	}

	public void accept(int selectedRow) {
		if (selectedRow < 0)
			return;

		if (fileVector.elementAt(selectedRow) instanceof FileTransferRequest) {
			FileTransferRequest request = (FileTransferRequest) fileVector
					.elementAt(selectedRow);
			IncomingFileTransfer transfer = request.accept();

			String fileDes = request.getDescription();
			String dir;
			File file;

			if (fileDes.startsWith("Plugin"))
				dir = "plugins";
			else
				dir = "files";

			file = new File(ManageAccount.getPath() + dir);
			file.mkdir();

			file = new File(ManageAccount.getPath() + dir
					+ ManageAccount.getFileSeparator() + transfer.getFileName());

			if (file.exists())
				file.delete();

			try {
				transfer.recieveFile(file);
			} catch (XMPPException e) {
				e.printStackTrace();
			}
			fileVector.remove(selectedRow);
			fileVector.insertElementAt(transfer, selectedRow);

			setValueAt(file.getAbsolutePath(), selectedRow, 5);
			setValueAt("Recieving", selectedRow, 3);
		}
	}
}
